package etnorservice.etnor_service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import etnorservice.etnor_service.domain.MenuItemDomain;
import etnorservice.etnor_service.domain.RoleUserDomain;
import etnorservice.etnor_service.domain.UserDomain;
import etnorservice.etnor_service.result.Result;
import etnorservice.etnor_service.service.UserManagementService;
import etnorservice.etnor_service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2025/03/24/13:05
 **/
@RestController
public class UserManagementController {

    @Autowired
    private UserManagementService userService;

    @GetMapping("/system/user/list")
    public Result getUserList(@RequestParam(defaultValue = "1") Integer current,
                              @RequestParam(defaultValue = "10") Integer size,
                              @RequestParam(required = false) String keyword) {
        Page<UserDomain> page = new Page<>(current, size);
        IPage<UserDomain> userList = userService.getUserList(page, keyword);
        Result result = new Result();
        result.setData(userList);
        return result;
    }

    @PostMapping("/system/user/create")
    public Result createUser(@RequestBody UserDomain user) {
        UserDomain newUser = userService.createUser(user);
        Result result = new Result();
        result.setData(newUser);
        return result;
    }

    @GetMapping("/system/user/{id}/role/list")
    public Result getUserRoleList(@PathVariable("id") Integer userId) {
        if (userId == null || userId <= 0) {
            return Result.error("无效的用户ID");
        }
        try {
            List<RoleUserDomain> roles = userService.getUserRoleList(userId);
            Result result = new Result();
            System.out.println("==========>"+roles);
            result.setData(roles);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取用户角色失败");
        }
    }

    @GetMapping("/system/{id}/menus")
    public Result getUserMenus(@PathVariable Integer id) {
        List<MenuItemDomain> menus = userService.getUserMenus(id);
        Result result = new Result();
        result.setData(menus);
        return result;
    }
    // 获取所有角色列表的接口
    @GetMapping("/system/role/list")
    public Result getAllRoles() {
        List<RoleUserDomain> roles = userService.getAllRoles();
        Result result = new Result();
        result.setData(roles);
        return result;
    }


    @PostMapping("/system/user/{id}/roles")
    public Result assignRoles(@PathVariable("id") Integer userId,
                              @RequestBody Map<String, Object> params) {
        try {
            if (userId == null || userId <= 0) {
                return Result.error("无效的用户ID");
            }

            List<Integer> roleIds = (List<Integer>) params.get("roleIds");
            List<String> roleNames = (List<String>) params.get("roleNames");

            if (roleIds == null) {
                roleIds = new ArrayList<>();
            }
            if (roleNames == null) {
                roleNames = new ArrayList<>();
            }

            userService.assignUserRoles(userId, roleIds, roleNames);
            return Result.success("角色分配成功");
        } catch (Exception e) {
            return Result.error("角色分配失败: " + e.getMessage());
        }
    }
}
